package org.example.cdpcrpbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.cdpcrpbackend.controller.dto.AdminInfo;
import org.example.cdpcrpbackend.domain.Admin;
import org.example.cdpcrpbackend.mapper.AdminMapper;
import org.example.cdpcrpbackend.service.AdminService;
import org.example.cdpcrpbackend.util.MyType;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Override
    public AdminInfo login(String username, String password) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername, username);
        wrapper.eq(Admin::getPassword, password);
        Admin admin = this.getOne(wrapper);
        if (admin == null) {
            return null;
        } else {
            // 拷贝属性
            AdminInfo info = new AdminInfo();
            info.setId(admin.getId());
            info.setUsername(admin.getUsername());
            info.setRole(MyType.RoleAdmin);
            return info;
        }
    }
}
